﻿using UnityEngine;
using GDGeek;
using System;
using System.Collections.Generic;
using MrPP.UX;
using static MrPP.SerriedCabinet.CabinetGroup;

namespace MrPP.SerriedCabinet
{
    public class Box : MonoBehaviour{

        [SerializeField]
        private Transform _cube;

      
        public Transform cube
        {
            get
            {
                return _cube;
            }
        }


        [SerializeField]
        private Transform _target;
        public Transform target
        {
            get
            {
                return _target;
            }
        }
        [System.Serializable]
        public class Data
        {
            public float channel;
            public int scale;
            public string name;
            public int area;
            public int index;
            public int type;
            public int layers;
            public int[] left;
            public int[] right;
            public float width;
         
        }
      
        public Cell getCell(BoxLocation location)
        {
            Tuple<Type, int, int> posi = GetPosition(location);

        //    Debug.LogError(_map);
//            Debug.LogError(posi);
            if (posi != null && _map.ContainsKey(posi)) {
           //     Debug.LogError(posi);
                return _map[posi];
            }
            return null;

        }

        private Tuple<Type, int, int> GetPosition(BoxLocation location)
        {

            return new Tuple<Type, int, int>(location.type, location.sile, location.layer);
           
        }

        public enum Type { 
            Left,
            Right,
            None,
        }
        private Dictionary<Tuple<Type, int/*sile*/, int/*layer*/>, Cell> _map = new Dictionary<Tuple<Type, int, int>, Cell>();
        public Dictionary<Tuple<Type, int, int>, Cell> map {
            get {
                return _map;
            }
        }
        private Data data_ = null;
        public Data data{
            get {
                return data_;
            }
        }

        [SerializeField]
        private Cell _phototype = null;
        private int box_ = -1;
        public void setup(int box, Data data, float height, float deep)
        {

            box_ = box;
            Renderer renderer = _cube.gameObject.GetComponent<Renderer>();
          
            cube.transform.localScale = new Vector3(data.width, height, deep);
     
            data_ = data;
            refresh();
        }

        public void refresh() {

           
            foreach (var item in _map) {
                DestroyImmediate(item.Value.gameObject);
            }
            _map.Clear();

            for (int layer = 0; layer < data_.layers; ++layer) {
                for (int l = 0; l < data_.left.Length; ++l)
                {
                    Cell left = GameObject.Instantiate(_phototype);
                    _map.Add(new Tuple<Type, int, int>(Type.Left, l, layer), left);
                    setupCell(left, box_,  Type.Left, l, layer);
                }
                for (int r = 0; r < data_.right.Length; ++r)
                {
                    Cell right = GameObject.Instantiate(_phototype);
                    _map.Add(new Tuple<Type, int, int>(Type.Right, r, layer), right);
                    setupCell(right, box_, Type.Right, r, layer);
                }
            }


        }

        internal void setPosition(float posi)
        {
            this._cube.localPosition = new Vector3(posi, 0, 0);
        }

        private void setupCell(Cell cell, int box, Type type, int sile, int layer)
        {
            cell.type = type;
            cell.layer = layer;
            cell.sile = sile;
            cell.box = box;


            cell.transform.SetParent(_cube.transform, false);
            float width = 0.5f;   
            float height = (1f) / ((float)(this.data_.layers));
            if (type == Type.Left) {

                float deep = (1f) / ((float)(this.data_.left.Length));
                cell.transform.localScale = new Vector3(width, height, deep) * 0.95f;
                cell.transform.localPosition = new Vector3(-width/2, (float)(layer) / (float)(this.data_.layers) + height / 2f - 0.5f , (float)(sile) / (float)(this.data_.left.Length) + deep / 2f - 0.5f);
            }
            else {
                          
                float deep = (1f) / ((float)(this.data_.right.Length));
                cell.transform.localScale = new Vector3(width, height, deep) * 0.95f;
                cell.transform.localPosition = new Vector3(width/2, (float)(layer) / (float)(this.data_.layers) + height / 2f - 0.5f, (float)(sile) / (float)(this.data_.right.Length) + deep / 2f - 0.5f);
            }
            cell.gameObject.SetActive(true);
        }
        public void lowlight() {
            foreach (var kv in this._map)
            {
                kv.Value.hightlight.lowlight();
            }
        }
        public void highlight(CabinetGroup.ItemData data)
        {
            Debug.LogError(data.position.x + "|" +data.position.y);
            foreach (var kv in this._map) { 
                
                if(kv.Value.type == data.type && kv.Value.sile == data.position.x && kv.Value.layer == data.position.y){
                    kv.Value.hightlight.highlight(data);
                }else {
                    kv.Value.hightlight.lowlight();
                }
            }
        }
    }
}